/*
 * StoreWriter.java
 * 
 * Created on Sep 30, 2007, 1:55:48 AM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package passvault.resources;

/**
 *
 * @author patrick
 */
public class StoreWriter {

    public String[] options = null;
    public String filename = null;
    public String fileformat = null;
    public PasswordStore pws = null;
    public String password = null;
    public boolean do_encrypt = false;

    public StoreWriter(String[] in_options,PasswordStore in_pws) {
        options = in_options;
        filename = options[0];
        fileformat = options[1];
        if (options.length == 3) {
            password = options[2];
            if ( password.length() > 0) { do_encrypt = true; }
        }
        pws = in_pws;
    }

    public boolean valid_data() {
        // Stub to validate data
        return true;
    }

    public boolean write_store_data() {
        boolean result = true;
        String output_text = null;
        if (options[1].equals("xml")) {
            output_text = make_xml();
        }
        if (do_encrypt) { output_text = encrypt_data(output_text); }
        write_out(filename, output_text);
        return result;
    }
    
    private void write_out(String filename, String text) {
        ReadFile file_writer = new ReadFile(filename);
        file_writer.write_out(text);
    }
    
    private String encrypt_data(String plaintext) {
        String output = null;
        AES aes = new AES();
        aes.init(password);
        output = aes.encrypt(plaintext);
        return output;
    }

    private String make_xml() {
        boolean result = false;
        XMLWriter xml = new XMLWriter();
        String output_xml = null;
        output_xml = xml.make_xml(pws);
//        output_xml = encrypt_data(output_xml);
        return output_xml;
    }
}
